﻿@model Matrix.MaKaYunShang.Model.ConsumerAddress
@{
    ViewBag.Title = "修改地址";
    ViewBag.PathToBtn = "/UserAddress/List/" + @ViewBag.ConsumerId;
    ViewBag.TextBtn = "返回至列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                店铺信息
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("", "", FormMethod.Post, new { id = "frmCreate", enctype = "multipart/form-data" }))
                    {
                        @Html.HiddenFor(model => model.Id)
                        <div class="col-lg-12">
                            <fieldset>
                                <div class="form-group col-md-6">
                                    <label>联系人姓名</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.LinkMan, new { @class = "form-control", placeholder = "联系人姓名" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>联系人电话</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.LinkMobile, new { @class = "form-control", placeholder = "联系人电话" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                @*<div class="form-group col-md-6 DiscountValue">
                                        <label>地区</label>
                                        <div class="input-group">
                                            @Html.TextBoxFor(model => model.AreaId, new { @class = "form-control", placeholder = "地址" })
                                            <span class="input-group-addon red">必填</span>
                                        </div>
                                    </div>*@
                                <div class="form-group col-md-12">
                                    <label>地址</label>
                                    <div class="input-group">
                                        当前已存储的地址： @Html.DisplayFor(model => model.Address, new { @class = "form-control" })
                                        @Html.TextBoxFor(model => model.Address, new { @class = "form-control", placeholder = "地址" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                    <br />
                                    <div style="border: 1px solid gray; min-height: 200px" id="mapContainer"></div>
                                    <a class="btn btn-primary" id="btnclear">清除选择</a>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>是否默认地址</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsDefault, new { @class = ""})

                                    </div>
                                </div>
                                <div class="form-group col-md-6 DiscountValue">
                                    <label>门牌号</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.AddressDetail, new { @class = "form-control", placeholder = "门牌号" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>经度</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Longitude, new { @class = "form-control", @readOnly = "readOnly", placeholder = "经度" })
                                        <span class="input-group-addon">°E</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>纬度</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.Latitude, new { @class = "form-control", @readOnly = "readOnly", placeholder = "纬度" })
                                        <span class="input-group-addon">°N</span>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <button id="btnSave" type="button" class="btn btn-primary">保存</button>
                                    <button id="btnReset" type="reset" class="btn btn-outline btn-primary">清空设置</button>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
                <!-- /.row (nested) -->
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>
<div class="modal fade" id="ChangeShopModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 500px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">更改用户绑定店铺（<font color="red">仅显示已激活的店铺</font>）</h4>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 5px;">
                    <input type="text" class="form-control" placeholder="店铺名称" style="width: 409px;display: inline-block" id="shopKeyWords" />
                    <a class="btn btn-primary" onclick="searchShops();">查询</a>
                </div>
                <div class="table-responsive table-bordered" style="max-height: 250px;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>店铺名称</th>
                                <th style="text-align: center">操作</th>
                            </tr>
                        </thead>
                        <tbody id="shopsData"></tbody>
                    </table>
                    <input type="hidden" id="hidConsumerId" value="@ViewBag.ConsumerId" />
                    <input type="hidden" id="hidId" value="@ViewBag.ConsumerId" />
                </div>
            </div>
        </div>
    </div>
</div>

@section javascript{
    <!-- Baidu Map JavaScript -->
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=0lypPbMMUsMDyrLk2G1meqGD"> </script>
    <script src="/Content/admin/dist/js/baiduMapHelper.js"> </script>
    <script type="text/javascript">
        localBaiduMap.generate(Longitude, Latitude, Address, mapContainer, btnclear);
        $(function() {
            $("#frmCreate").validate({
                rules: {
                    LinkMan: { required: true },
                    LinkMobile: { required: true, isMobile: true },
                 //   Address: { required: true },
                    AddressDetail: { required: true },
                    Longitude: { required: true },
                    Latitude: { required: true },
                },
                messages: {
                    LinkMan: { required: "请填写联系人姓名" },
                    LinkMobile: { required: "请填写联系人电话" },
                    Address: { required: "请填写地址" },
                    AddressDetail: { required: "请填写门牌号" },
                    Longitude: { required: "请选择经度" },
                    Latitude: { required: "请选择纬度" },
                },
                submitHandler: function(form) {
                    // form.submit();
                }
            });
            $("#btnSave").click(function() {
                if ($("#frmCreate").valid()) {
                    $.ajax({
                        type: 'post',
                        async: false,
                        url: '/UserAddress/Update',
                        data: $('#frmCreate').serialize(),
                        success: function(result) {
                            if (result.Status) {
                                $("#hidId").val(result.Message);
                                alertify.success("修改成功");
                                alertify.okBtn("更换").cancelBtn("取消").confirm("是否更换店铺？", function() {
                                    searchShops();
                                }, function() {
                                    setTimeout(function() { location.href = "/UserAddress/List/" + @ViewBag.ConsumerId; }, 1000);
                                });
                            } else {
                                alertify.error("添加失败");
                            }

                        }
                    });
                }
            });
        });

        function searchShops() {
            $("#shopsData").html("");
            $.post("@Url.Action("GetActiveShopList")", { keyWords: encodeURIComponent($("#shopKeyWords").val()),id:$("#hidId").val()}, function(data) {
                if (data != null&&data!='') {
                    $('#ChangeShopModal').modal('show');
                    for (var i = 0; i < data.length; i++) {
                        $("#shopsData").append("<tr><td>" + data[i].Name + "</td><td align='center'><a class='btn btn-info' onclick='confirmChangeShop(" + data[i].Id + ");'>绑定</a></td></tr>");
                    }
                } else {
                    alertify.alert("未找到范围内的店铺");
                }
            });
        }
        function confirmChangeShop(shopId) {
            alertify.okBtn("确定").cancelBtn("取消").confirm("确定将此用户绑定到此店铺吗？", function() {
                $.post("@Url.Action("ChangeShop", "Consumer")", "consumerId=" + $("#hidConsumerId").val() + "&shopId=" + shopId, function(data) {
                    if (data.Status) {
                        $("#ChangeShopModal").modal("hide");
                        alertify.success("绑定成功");
                        setTimeout(function() { location.href = "/UserAddress/List/" + @ViewBag.ConsumerId; }, 1000);
                    } else {
                        alertify.alert(data.Message);
                    }
                });
            }, function() {
                setTimeout(function() { location.reload(); }, 1000);
            });
        }
    </script>
}